package com.studyweb.service.impl;

import com.studyweb.mapper.PaperResultMapper;
import com.studyweb.pojo.entity.PaperResult;
import com.studyweb.service.PaperResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Service
public class PaperResultServiceImpl implements PaperResultService {
    
    private static final Logger logger = LoggerFactory.getLogger(PaperResultServiceImpl.class);
    
    @Autowired
    private PaperResultMapper paperResultMapper;
    
    @Override
    public PaperResult createPaperResult(PaperResult paperResult) {
        try {
            logger.info("开始创建试卷测验结果: userId={}, paperId={}", paperResult.getUserId(), paperResult.getPaperId());
            logger.debug("试卷测验结果详情: {}", paperResult);
            
            // 检查必要字段
            if (paperResult.getUserId() == null || paperResult.getPaperId() == null) {
                throw new IllegalArgumentException("缺少必要字段: userId或paperId为空");
            }
            
            paperResultMapper.insert(paperResult);
            logger.info("试卷测验结果创建成功: id={}", paperResult.getId());
            return paperResult;
        } catch (Exception e) {
            logger.error("创建试卷测验结果失败", e);
            throw new RuntimeException("创建试卷测验结果失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public PaperResult getPaperResultById(Long id) {
        try {
            logger.info("开始获取试卷测验结果: id={}", id);
            PaperResult result = paperResultMapper.selectById(id);
            logger.info("获取试卷测验结果{}: id={}", result != null ? "成功" : "失败", id);
            return result;
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            throw new RuntimeException("获取试卷测验结果失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public List<PaperResult> getPaperResultsByUserIdAndPaperId(Long userId, Long paperId) {
        try {
            logger.info("开始获取试卷测验结果: userId={}, paperId={}", userId, paperId);
            List<PaperResult> results = paperResultMapper.selectByUserIdAndPaperId(userId, paperId);
            logger.info("获取试卷测验结果成功: userId={}, paperId={}, 结果数量={}", userId, paperId, results.size());
            return results;
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            throw new RuntimeException("获取试卷测验结果失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public List<PaperResult> getPaperResultsByUserId(Long userId) {
        try {
            logger.info("开始获取试卷测验结果: userId={}", userId);
            List<PaperResult> results = paperResultMapper.selectByUserId(userId);
            logger.info("获取试卷测验结果成功: userId={}, 结果数量={}", userId, results.size());
            return results;
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            throw new RuntimeException("获取试卷测验结果失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public PaperResult updatePaperResult(PaperResult paperResult) {
        try {
            logger.info("开始更新试卷测验结果: id={}", paperResult.getId());
            
            // 检查必要字段
            if (paperResult.getId() == null) {
                throw new IllegalArgumentException("缺少必要字段: id为空");
            }
            
            paperResultMapper.update(paperResult);
            logger.info("试卷测验结果更新成功: id={}", paperResult.getId());
            return paperResult;
        } catch (Exception e) {
            logger.error("更新试卷测验结果失败", e);
            throw new RuntimeException("更新试卷测验结果失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public void deletePaperResult(Long id) {
        try {
            logger.info("开始删除试卷测验结果: id={}", id);
            
            // 检查必要字段
            if (id == null) {
                throw new IllegalArgumentException("缺少必要字段: id为空");
            }
            
            paperResultMapper.deleteById(id);
            logger.info("试卷测验结果删除成功: id={}", id);
        } catch (Exception e) {
            logger.error("删除试卷测验结果失败", e);
            throw new RuntimeException("删除试卷测验结果失败: " + e.getMessage(), e);
        }
    }
}